package com.qc.dao.impl;

import java.util.List;

import javax.annotation.Resource;

import org.springframework.jdbc.core.BeanPropertyRowMapper;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Repository;

import com.qc.common.StringUtil;
import com.qc.dao.BusinessPeopleDao;
import com.qc.entity.BusinessPeopleVo;

@SuppressWarnings({ "unchecked", "rawtypes" })
@Repository("businessPeopleDao")
public class BusinessPeopleDaoImpl implements BusinessPeopleDao{
	
	@Resource(name="jdbcTemplate")
	private JdbcTemplate jdbcTemplate;

	@Override
	public int getListBusinessJobVoCount(long shopId,String nickName,int state) {

		String sql = "select count(*) from t_business_people a"
				+ " left join t_user b on b.user_id = a.user_id"
				+ " where a.shop_id = "+shopId;
		
			if (StringUtil.isNotEmpty(nickName) && !"undefined".equals(nickName) ) {
	            sql += " and b.nickname like '%"+nickName+"%' ";
	        }
			
			if (state != 0) {
				sql += " and a.state =" + state;
			}
			
			return jdbcTemplate.queryForInt(sql);
	}

	@Override
	public List<BusinessPeopleVo> getAllBusinessPeople(long shopId,String nickName,int state,int pageNo, int pageSize) {
		
		int beginRow = (pageNo - 1)* pageSize;
		
		String sql = "select a.*,b.*,c.* from t_business_people a "
				+ " left join t_user b on b.user_id = a.user_id "
				+ " left join t_user_secretary c on c.user_id = b.user_id "
				+ " where shop_id = "+shopId;
			
			if (StringUtil.isNotEmpty(nickName) && !"undefined".equals(nickName) ) {
	            sql += " and b.nickname like '%"+nickName+"%' ";
	        }
			
			if (state != 0) {
				sql += " and a.state =" + state;
			}
			
		 	sql += " order by a.created_time DESC ";
	        sql += " LIMIT "+ String.valueOf(beginRow)+ "," + pageSize;
	        
	        return jdbcTemplate.query(sql, new BeanPropertyRowMapper(BusinessPeopleVo.class));
	}

	@Override
	public int updatePeopleId(long bId,int state) {
		
		String sql = "update t_business_people set state = '"+state+"' where b_id = " +bId;
		
        return jdbcTemplate.update(sql);
	}

	@Override
	public BusinessPeopleVo getBusinessPeopleVoId(long bId) {
		String sql = "select a.*,b.*,c.* from t_business_people a "
				+ " left join t_user b on b.user_id = a.user_id "
				+ " left join t_user_secretary c on c.user_id = b.user_id "
				+ " where b_id = "+bId;
		
		return (BusinessPeopleVo) jdbcTemplate.queryForObject(sql, new BeanPropertyRowMapper(BusinessPeopleVo.class));
	}

}
